package huaweiod.first;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 华为OD机试真题 Java 实现【整理扑克牌】【2022.11 Q4 新题】
 * 1 3 3 3 2 1 5
 * 3 3 3 1 1 5 2
 *
 * 4 4 2 1 2 1 3 3 3 4
 */
public class A_2023_46_PackCards {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        List<String> collect = Arrays.stream(in.nextLine().split(" ")).collect(Collectors.toList());
        Map<String,Integer> map = new HashMap<>();
        for (String key : collect) {
            map.put(key, map.getOrDefault(key,0) + 1);
        }
        List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
        list.sort((a,b) ->{
            if(a.getValue().equals(b.getValue())) {
                return b.getKey().compareTo(a.getKey());
            } else {
                return b.getValue() - a.getValue();
            }
        });
        StringJoiner sj = new StringJoiner(" ");
        LinkedList<String> singleCard = new LinkedList<>();
        for (int i = 0; i < list.size(); i++) {
            Map.Entry<String, Integer> entry = list.get(i);
            String key = entry.getKey();
            int value = entry.getValue();
            if(i > 0 && value == 3 && list.get(i - 1).getValue() == 3) {
                sj.add(key);
                sj.add(key);
                singleCard.add(key);
                entry.setValue(2);
            } else if(value == 1 && singleCard.size() > 0) {
                for (int k = 0; k < singleCard.size(); k++) {
                    if(singleCard.get(k).compareTo(key) > 0) {
                        sj.add(singleCard.get(k));
                        singleCard.remove(k--);
                    }
                }
            } else {
                for (int j = 0; j < value; j++) {
                    sj.add(key);
                }
            }
        }
        for (int i = 0; i < singleCard.size(); i++) {
            sj.add(singleCard.get(i));
        }
        System.out.println(sj);
    }
}
